import java.util.Random;
import java.util.concurrent.CountDownLatch;


public class Demo2 {
    //定义选手人数
    private static int playerCount = 5;
    //定义准备就绪倒计时
    private static CountDownLatch standby = new CountDownLatch(playerCount);
    //定义起跑的倒计时
    private static CountDownLatch start = new CountDownLatch(1);
    //定义选手全部跑完的倒计时
    private static CountDownLatch finish = new CountDownLatch(playerCount);

    //定义选手类
    static class Runner implements Runnable{
        @Override
        public void run() {
            //准备就绪的工作
            System.out.println(Thread.currentThread().getName()+"准备就绪");
            standby.countDown();
            try {
                start.await();
                System.out.println(Thread.currentThread().getName()+"出发");
                Random random = new Random();
                int time = random.nextInt(10) + 10;
                System.out.println(Thread.currentThread().getName()+"使用了"+time+"秒到达了终点");
                finish.countDown();
            } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        //选手人数准备起跑
        for (int i = 0; i < playerCount; i++) {
            new Thread(new Runner(),"player"+(i+1)).start();
        }
        standby.await();
        System.out.println("预备，跑");
        start.countDown();
        finish.await();
        System.out.println("跑步结束");
    }
}
